package com.tcp2.handler;

/**
 * Author: 杨俊辉
 * Time: 2014-09-22 17:35
 * Copyright (C) 2014 Xiamen Yaxon Networks CO.,LTD.
 */

import com.tcp2.server.ProtoMsg;
import com.tcp2.util.ProtoUtil;
import com.yaxon.vndp.common.util.CodecUtils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;



/**
 * 车辆静态信息交换处理类
 */
@Service
public class VehicleBaseMsgExchangeHandler extends BaseMsgHandler {

    protected static Logger logger = LoggerFactory.getLogger(VehicleBaseMsgExchangeHandler.class);


    @Autowired
    private TenantCacheService tenantCacheService;

    @Autowired
    private PlatformCacheServcie platformCacheServcie;

    @Override
    public void handle(ProtoMsg msg) {

        logger.info("-> 收到主链路， 车辆静态信息交换处理类，消息id：0x{}，下级平台接入码：{}", CodecUtils.shortToHex((short) msg.msgId), msg.gnssCenterId);

        ByteBuf data = msg.msgData;
        String lpn = ProtoUtil.readOctetString(data, 21);
        byte color = data.readByte();
        int dataType = data.readUnsignedShort(); //数据类型
        int dataLen = data.readInt();//后续数据长度
        if (dataLen != data.readableBytes()) {
            logger.warn("(UP_BASE_MSG):数据长度不一致");
            return;
        }
        switch (dataType) {
            case 1:
                doUpBaseMsgVehicleAddedAck(lpn, color, data ,msg.gnssCenterId, dataType);
                break;
            default:
                throw new RuntimeException("未知子业务类型：0x" + CodecUtils.shortToHex((short) dataType));

        }
    }


    private void doUpBaseMsgVehicleAddedAck(String lpn, byte color, ByteBuf data, int gnssCenterId, int dataType) {
        logger.info("-> 收到主链路，补报车辆静态信息应答，子业务类型 ：0x{}", CodecUtils.shortToHex((short) dataType));
        logger.info("字节数{}，16进制:{}",data.readableBytes() ,ByteBufUtil.hexDump(data).replaceAll("\\w{2}(?!$)", "$0 ").toUpperCase());
        try {
            String carInfo = ProtoUtil.readString(data, data.readableBytes());


        } catch (Exception e) {
            logger.warn("补报车辆静态信息应答失败", e);
        }
    }

}
